package object;

/**
 * 面向对象编程(基础部分)练习
 */
public class ObjectExercise01 {

    public static void main(String[] args) {
        /*
            1)编写类A01,定义方法max,实现求某个double数组的最大值，并返回
            2)编写类A02,定义方法find,实现查找某字符串是否在字符串数组中，并返回索引，如果找不到返回-1
         */
        A01 a01 = new A01();
        double[] arr = {1.1, 2.2};
//        double[] arr = {};
        Double res = a01.max(arr);
        if (res != null) {
            System.out.println("arr的最大值=" + res);
        } else {
            System.out.println("arr输入有误");
        }

        String[] str = {"jack", "tom", "mary"};
        A02 a02 = new A02();
        int i = a02.find(str, "milan");
        if (i != -1) {
            System.out.println("查找的index=" + i);
        } else {
            System.out.println("没有找到index");
        }

        Book book = new Book("笑傲江湖", 300);
        book.info();//300
        book.updatePrice();
        book.info();//150
    }
}

class A01 {

    public Double max(double[] arr) {
        if (arr.length > 0) {
            double max = arr[0];
            for (double v : arr) {
                if (v > max) {
                    max = v;
                }
            }
            return max;//double值
        } else {
            return null;
        }
    }
}

class A02 {

    public int find(String[] arr, String key) {
        if (arr.length > 0 && key != null) {
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].equals(key)) {
                    return i;
                }
            }
            return -1;
        } else {
            return -1;
        }
    }
}

class Book {

    String name;

    double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }
    public void updatePrice() {
        // 如果方法中没有 price 局部变量，this.price等价于price
        if (this.price > 150) {
            this.price = 150;
        } else if (this.price > 100) {
            this.price = 100;
        }
    }
    public void info() {
        System.out.println("书名：" + this.name + "，价格=" + this.price);
    }
}